package com.iknow.net;

import com.iknow.util.IKFileUtil;
import com.iknow.util.LogUtil;
import com.iknow.util.StringUtil;
import com.iknow.util.SystemUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

public class NetFileInfo extends InputStream
{
  private RandomAccessFile accessFile;
  private long currentLength;
  private File file;
  private long fileLength;
  private String filePath;
  private boolean isStop;

  public NetFileInfo()
  {
    this.filePath = null;
    this.file = null;
    this.accessFile = null;
    this.fileLength = -1L;
    this.currentLength = 0L;
    this.isStop = false;
    String str = SystemUtil.fillSDTempPath("");
    new File(str).mkdirs();
    try
    {
      this.file = File.createTempFile("net_", ".tmp", new File(str));
      this.filePath = this.file.getPath();
      return;
    }
    catch (IOException localIOException)
    {
      LogUtil.e(NetFileInfo.class, localIOException);
    }
  }

  public NetFileInfo(File paramFile)
  {
    this.filePath = null;
    this.file = null;
    this.accessFile = null;
    this.fileLength = -1L;
    this.currentLength = 0L;
    this.isStop = false;
    if ((paramFile != null) && (paramFile.exists()))
    {
      this.filePath = paramFile.getPath();
      this.file = paramFile;
      return;
    }
    throw new NullPointerException("File Is Null !!");
  }

  public NetFileInfo(InputStream paramInputStream)
  {
    if (getFile() != null)
      IKFileUtil.writeFile(paramInputStream, this.file);
  }

  public NetFileInfo(String paramString)
  {
    this.filePath = null;
    this.file = null;
    this.accessFile = null;
    this.fileLength = -1L;
    this.currentLength = 0L;
    this.isStop = false;
    if (StringUtil.isEmpty(paramString))
      throw new NullPointerException("File Name Is Null !!");
    this.filePath = paramString;
  }

  private int writeToFile(long paramLong, InputStream paramInputStream)
  {
    int i;
    if ((isStop()) || (!(ready())) || (paramInputStream == null) || (paramLong < 0L))
      i = -1;
    while (true)
    {
      return i;
      if (!(isFill()))
        break;
      i = -1;
    }
    int j = 0;
    try
    {
      seek(paramLong);
      byte[] arrayOfByte = new byte[1024];
      int k = paramInputStream.read(arrayOfByte);
      if (k == -1)
        break label126:
      j += k;
      getAccessFile().write(arrayOfByte, 0, k);
      label126: this.currentLength += k;
    }
    catch (Exception localException)
    {
      while (true)
      {
        LogUtil.e(NetFileInfo.class, localException);
        j = -1;
        i = j;
      }
    }
  }

  public void close()
  {
    if (ready());
    try
    {
      if ((getAccessFile() != null) && (getFile() != null))
        this.currentLength = getFile().length();
      getAccessFile().close();
      this.isStop = true;
      return;
    }
    catch (IOException localIOException)
    {
      LogUtil.e(NetFileInfo.class, localIOException);
    }
  }

  public long currentLength()
  {
    long l;
    if (this.currentLength > 0L)
    {
      l = this.currentLength;
      return l;
    }
    if (getAccessFile() != null);
    try
    {
      this.currentLength = getAccessFile().length();
      l = this.currentLength;
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        while (true)
        {
          LogUtil.e(NetFileInfo.class, localIOException);
          l = -1L;
        }
        if (getFile() == null)
          break;
        this.currentLength = getFile().length();
        l = this.currentLength;
      }
      l = -1L;
    }
  }

  public long currentMaxlength()
  {
    return this.fileLength;
  }

  public boolean delete()
  {
    boolean bool;
    close();
    if ((ready()) && (getFile().exists()))
      bool = getFile().delete();
    while (true)
    {
      return bool;
      bool = false;
    }
  }

  public boolean exists()
  {
    boolean bool;
    if (getFile() != null)
      bool = getFile().exists();
    while (true)
    {
      return bool;
      bool = false;
    }
  }

  public RandomAccessFile getAccessFile()
  {
    if (this.accessFile == null);
    try
    {
      this.accessFile = new RandomAccessFile(this.file, "rw");
      return this.accessFile;
    }
    catch (FileNotFoundException localFileNotFoundException)
    {
      LogUtil.e(NetFileInfo.class, localFileNotFoundException);
      this.accessFile = null;
    }
  }

  public File getFile()
  {
    File localFile;
    if (StringUtil.isEmpty(this.filePath))
    {
      localFile = null;
      return localFile;
    }
    try
    {
      if (this.file == null)
      {
        this.file = new File(this.filePath);
        if (!(this.file.exists()))
          this.file.createNewFile();
      }
      if ((!(this.file.exists())) && (!(this.file.canRead())) && (!(this.file.canWrite())))
        this.file = null;
      localFile = this.file;
    }
    catch (Exception localException)
    {
      while (true)
      {
        LogUtil.e(NetFileInfo.class, localException);
        this.file = null;
      }
    }
  }

  public String getPath()
  {
    String str;
    if (getFile() != null)
      str = this.file.getPath();
    while (true)
    {
      return str;
      str = null;
    }
  }

  public boolean isFill()
  {
    int i;
    long l1 = currentMaxlength();
    long l2 = currentLength();
    if ((l1 < 0L) || (l2 < 0L))
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        if (l1 > l2)
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public boolean isStop()
  {
    return this.isStop;
  }

  public long length()
  {
    long l;
    if (getFile() != null)
      l = getFile().length();
    while (true)
    {
      return l;
      l = 0L;
    }
  }

  public int read()
    throws IOException
  {
    int i;
    if (ready())
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public boolean ready()
  {
    int i;
    if (this.isStop)
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        if (getAccessFile() == null)
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public void reset()
  {
    delete();
    this.isStop = false;
  }

  public boolean seek(long paramLong)
  {
    int i;
    if (ready());
    try
    {
      getAccessFile().seek(paramLong);
      i = 1;
      return i;
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        LogUtil.e(NetFileInfo.class, localIOException);
        i = 0;
      }
      i = 0;
    }
  }

  public int write(long paramLong, InputStream paramInputStream)
  {
    int i;
    if ((paramLong < 0L) || (paramInputStream == null) || (isStop()))
    {
      i = -1;
      return i;
    }
    int j = writeToFile(paramLong, paramInputStream);
    try
    {
      paramInputStream.close();
      i = j;
    }
    catch (IOException localIOException)
    {
      while (true)
        LogUtil.e(NetFileInfo.class, localIOException);
    }
  }

  // ERROR //
  public long write(long paramLong1, InputStream paramInputStream, long paramLong2)
  {
    // Byte code:
    //   0: lload 4
    //   2: lconst_0
    //   3: lcmp
    //   4: ifgt +15 -> 19
    //   7: aload_0
    //   8: lload_1
    //   9: aload_3
    //   10: invokevirtual 174	com/iknow/net/NetFileInfo:write	(JLjava/io/InputStream;)I
    //   13: i2l
    //   14: lstore 8
    //   16: lload 8
    //   18: lreturn
    //   19: lconst_0
    //   20: lstore 6
    //   22: lload_1
    //   23: lconst_0
    //   24: lcmp
    //   25: iflt +14 -> 39
    //   28: aload_0
    //   29: invokevirtual 105	com/iknow/net/NetFileInfo:isStop	()Z
    //   32: istore 15
    //   34: iload 15
    //   36: ifeq +31 -> 67
    //   39: aload_3
    //   40: ifnull +7 -> 47
    //   43: aload_3
    //   44: invokevirtual 171	java/io/InputStream:close	()V
    //   47: ldc2_w 28
    //   50: lstore 8
    //   52: goto -36 -> 16
    //   55: astore 10
    //   57: ldc 2
    //   59: aload 10
    //   61: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   64: goto -17 -> 47
    //   67: sipush 10240
    //   70: newarray byte
    //   72: astore 16
    //   74: aload_0
    //   75: lload_1
    //   76: invokevirtual 115	com/iknow/net/NetFileInfo:seek	(J)Z
    //   79: pop
    //   80: aload_3
    //   81: aload 16
    //   83: invokevirtual 119	java/io/InputStream:read	([B)I
    //   86: istore 20
    //   88: iload 20
    //   90: ifle +43 -> 133
    //   93: lload 6
    //   95: iload 20
    //   97: i2l
    //   98: ladd
    //   99: lload 4
    //   101: lcmp
    //   102: iflt +57 -> 159
    //   105: lload 4
    //   107: lload 6
    //   109: lsub
    //   110: l2i
    //   111: istore 22
    //   113: aload_0
    //   114: invokevirtual 123	com/iknow/net/NetFileInfo:getAccessFile	()Ljava/io/RandomAccessFile;
    //   117: aload 16
    //   119: iconst_0
    //   120: iload 22
    //   122: invokevirtual 129	java/io/RandomAccessFile:write	([BII)V
    //   125: lload 6
    //   127: iload 22
    //   129: i2l
    //   130: ladd
    //   131: lstore 6
    //   133: aload_0
    //   134: lload 6
    //   136: aload_0
    //   137: getfield 33	com/iknow/net/NetFileInfo:currentLength	J
    //   140: ladd
    //   141: putfield 33	com/iknow/net/NetFileInfo:currentLength	J
    //   144: aload_3
    //   145: ifnull +7 -> 152
    //   148: aload_3
    //   149: invokevirtual 171	java/io/InputStream:close	()V
    //   152: lload 6
    //   154: lstore 8
    //   156: goto -140 -> 16
    //   159: aload_0
    //   160: invokevirtual 123	com/iknow/net/NetFileInfo:getAccessFile	()Ljava/io/RandomAccessFile;
    //   163: aload 16
    //   165: iconst_0
    //   166: iload 20
    //   168: invokevirtual 129	java/io/RandomAccessFile:write	([BII)V
    //   171: lload 6
    //   173: iload 20
    //   175: i2l
    //   176: ladd
    //   177: lstore 6
    //   179: goto -99 -> 80
    //   182: astore 18
    //   184: ldc 2
    //   186: aload 18
    //   188: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   191: aload_3
    //   192: ifnull +7 -> 199
    //   195: aload_3
    //   196: invokevirtual 171	java/io/InputStream:close	()V
    //   199: ldc2_w 28
    //   202: lstore 8
    //   204: goto -188 -> 16
    //   207: astore 19
    //   209: ldc 2
    //   211: aload 19
    //   213: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   216: goto -17 -> 199
    //   219: astore 13
    //   221: ldc 2
    //   223: aload 13
    //   225: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   228: aload_3
    //   229: ifnull -77 -> 152
    //   232: aload_3
    //   233: invokevirtual 171	java/io/InputStream:close	()V
    //   236: goto -84 -> 152
    //   239: astore 14
    //   241: ldc 2
    //   243: aload 14
    //   245: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   248: goto -96 -> 152
    //   251: astore 11
    //   253: aload_3
    //   254: ifnull +7 -> 261
    //   257: aload_3
    //   258: invokevirtual 171	java/io/InputStream:close	()V
    //   261: aload 11
    //   263: athrow
    //   264: astore 12
    //   266: ldc 2
    //   268: aload 12
    //   270: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   273: goto -12 -> 261
    //   276: astore 21
    //   278: ldc 2
    //   280: aload 21
    //   282: invokestatic 70	com/iknow/util/LogUtil:e	(Ljava/lang/Object;Ljava/lang/Object;)V
    //   285: goto -133 -> 152
    //
    // Exception table:
    //   from	to	target	type
    //   43	47	55	IOException
    //   80	125	182	IOException
    //   159	171	182	IOException
    //   195	199	207	IOException
    //   28	34	219	java/lang/Exception
    //   67	80	219	java/lang/Exception
    //   80	125	219	java/lang/Exception
    //   133	144	219	java/lang/Exception
    //   159	171	219	java/lang/Exception
    //   184	191	219	java/lang/Exception
    //   232	236	239	IOException
    //   28	34	251	finally
    //   67	80	251	finally
    //   80	125	251	finally
    //   133	144	251	finally
    //   159	171	251	finally
    //   184	191	251	finally
    //   221	228	251	finally
    //   257	261	264	IOException
    //   148	152	276	IOException
  }

  public long write(HttpResponse paramHttpResponse)
  {
    long l1;
    long l2;
    long l3;
    long l4;
    if (paramHttpResponse == null)
      l3 = -1L;
    while (true)
    {
      return l3;
      Header localHeader1 = paramHttpResponse.getFirstHeader("Content-Length");
      Header localHeader2 = paramHttpResponse.getFirstHeader("Content-Range");
      l1 = -1L;
      l2 = -1L;
      if (localHeader1 != null)
        l2 = StringUtil.toLong(localHeader1.getValue(), -1L);
      if (localHeader2 == null)
        break label264;
      String str1 = localHeader2.getValue();
      if (StringUtil.isEmpty(str1))
        break label264;
      String str2 = str1.trim();
      int i = str2.lastIndexOf(" ");
      int j = str2.lastIndexOf("-");
      int k = str2.lastIndexOf("/");
      if ((i <= 0) || (j <= 0) || (k <= 0) || (i >= j) || (i >= k) || (j >= k) || (k + 1 > str2.length()))
        break label264;
      l1 = StringUtil.toLong(str2.substring(i + 1, j), -1L);
      if (l2 < 0L)
        l2 = 1L + StringUtil.toLong(str2.substring(j + 1, k), -1L);
      l4 = StringUtil.toLong(str2.substring(k + 1), -1L);
      if ((this.fileLength <= 0L) || (this.fileLength == l4))
        break;
      LogUtil.e(NetFileInfo.class, "FileSize Change !!!");
      l3 = -1L;
    }
    this.fileLength = l4;
    if (l1 < 0L)
      label264: l1 = currentMaxlength();
    if (l1 < 0L)
      l1 = 0L;
    try
    {
      InputStream localInputStream = paramHttpResponse.getEntity().getContent();
      l3 = write(l1, localInputStream, l2);
    }
    catch (Exception localException)
    {
      LogUtil.e(NetFileInfo.class, localException);
      l3 = -1L;
    }
  }
}